<?php

defined('_JEXEC') or die;

class ExercisesViewAnswers extends JViewLegacy
{
	protected $items;
	protected $pagination;
	protected $state;

	/**
	 * Display the view
	 */
	public function display($tpl = null)
	{
		// Initialise variables.
		$this->items		= $this->get('Items');
		$this->pagination	= $this->get('Pagination');
		$this->state		= $this->get('State');
                $this->filterForm    = $this->get('FilterForm');
		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}
                 ExercisesHelper::addSubmenu('answers');
		$this->addToolbar();
                JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
                JHtml::_('exercise.stylesheet');
                $this->sidebar = JHtmlSidebar::render();
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addToolbar()
	{
		require_once JPATH_COMPONENT.'/helpers/exercises.php';

		$canDo	= ExercisesHelper::getActions();

		JToolBarHelper::title(JText::_('COM_EXERCISES_MANAGER_ANSWERS'), 'exercises-answers.png');
		if ($canDo->get('core.create')) {
			JToolBarHelper::addNew('answer.add');
		}
		if ($canDo->get('core.edit')) {
			JToolBarHelper::editList('answer.edit');
		}
		if ($canDo->get('core.edit.state')) {
			JToolBarHelper::divider();
			JToolBarHelper::publish('answers.publish', 'JTOOLBAR_PUBLISH', true);
			JToolBarHelper::unpublish('answers.unpublish', 'JTOOLBAR_UNPUBLISH', true);
			JToolBarHelper::divider();
			JToolBarHelper::archiveList('answers.archive');
			JToolBarHelper::checkin('answers.checkin');
		}
		if ($this->state->get('filter.state') == -2 && $canDo->get('core.delete')) {
			JToolBarHelper::deleteList('', 'answers.delete', 'JTOOLBAR_EMPTY_TRASH');
			JToolBarHelper::divider();
		} elseif ($canDo->get('core.edit.state')) {
			JToolBarHelper::trash('answers.trash');
			JToolBarHelper::divider();
		}

		if ($canDo->get('core.admin')) {
			JToolBarHelper::preferences('com_exercises');
			JToolBarHelper::divider();
		}

		JToolBarHelper::help('JHELP_COMPONENTS_EXERCISES_ANSWERS');
	}
         protected function getSortFields()
	{
		return array(
			'ordering' => JText::_('JGRID_HEADING_ORDERING'),
			'a.state' => JText::_('JSTATUS'),
			'a.name' => JText::_('COM_EXERCISES_HEADING_NAME'),
			'a.id' => JText::_('JGRID_HEADING_ID')
		);
	}
}
